{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 查看Pipeline支持的任务类型"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "from transformers.pipelines import SUPPORTED_TASKS"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "from pprint import pprint\n",
    "\n",
    "\n",
    "pprint(SUPPORTED_TASKS.keys())"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "for k, v in SUPPORTED_TASKS.items():\n",
    "\tprint(k, v)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pipeline的创建与使用方式"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "from transformers import pipeline, QuestionAnsweringPipeline"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 根据任务类型直接创建Pipeline, 默认都是英文的模型"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "pipe = pipeline(\"text-classification\")"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "pipe([\"very good!\", \"vary bad!\"])"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 指定任务类型，再指定模型，创建基于指定模型的Pipeline"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "# https://huggingface.co/models\n",
    "pipe = pipeline(\"text-classification\", model = \"uer/roberta-base-finetuned-dianping-chinese\")"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "pipe(\"我觉得不太行！\")"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 预先加载模型，再创建Pipeline"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "from transformers import AutoModelForSequenceClassification, AutoTokenizer\n",
    "\n",
    "\n",
    "# 这种方式，必须同时指定model和tokenizer\n",
    "model = AutoModelForSequenceClassification.from_pretrained(\"uer/roberta-base-finetuned-dianping-chinese\")\n",
    "tokenizer = AutoTokenizer.from_pretrained(\"uer/roberta-base-finetuned-dianping-chinese\")\n",
    "pipe = pipeline(\"text-classification\", model = model, tokenizer = tokenizer)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "pipe(\"我觉得现在的java行情不太好\")",
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "pipe.model.device"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import torch\n",
    "import time\n",
    "\n",
    "\n",
    "times = []\n",
    "for i in range(100):\n",
    "\ttorch.cuda.synchronize()\n",
    "\tstart = time.time()\n",
    "\tpipe(\"我觉得不太行！\")\n",
    "\ttorch.cuda.synchronize()\n",
    "\tend = time.time()\n",
    "\ttimes.append(end - start)\n",
    "print(sum(times) / 100)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用GPU进行推理"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "pipe = pipeline(\"text-classification\", model = \"uer/roberta-base-finetuned-dianping-chinese\", device = 0)",
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "pipe.model.device"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import torch\n",
    "import time\n",
    "\n",
    "\n",
    "times = []\n",
    "for i in range(100):\n",
    "\ttorch.cuda.synchronize()\n",
    "\tstart = time.time()\n",
    "\tpipe(\"我觉得不太行！\")\n",
    "\ttorch.cuda.synchronize()\n",
    "\tend = time.time()\n",
    "\ttimes.append(end - start)\n",
    "print(sum(times) / 100)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 确定Pipeline参数"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "qa_pipe = pipeline(\"question-answering\", model = \"uer/roberta-base-chinese-extractive-qa\")",
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "qa_pipe"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "QuestionAnsweringPipeline"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "qa_pipe(question = \"中国的首都是哪里？\", context = \"中国的首都是北京\", max_answer_len = 2)",
   "outputs": [],
   "execution_count": null
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 其他Pipeline示例"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "checkpoint = \"google/owlvit-base-patch32\"\n",
    "detector = pipeline(model = checkpoint, task = \"zero-shot-object-detection\")"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "from PIL import Image\n",
    "\n",
    "im = Image.open(\"1.jpg\")\n",
    "im"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "predictions = detector(\n",
    "\tim,\n",
    "\tcandidate_labels = [\"hat\", \"sunglasses\", \"book\"],\n",
    ")\n",
    "predictions"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "from PIL import ImageDraw\n",
    "\n",
    "\n",
    "draw = ImageDraw.Draw(im)\n",
    "\n",
    "for prediction in predictions:\n",
    "\tbox = prediction[\"box\"]\n",
    "\tlabel = prediction[\"label\"]\n",
    "\tscore = prediction[\"score\"]\n",
    "\txmin, ymin, xmax, ymax = box.values()\n",
    "\tdraw.rectangle((xmin, ymin, xmax, ymax), outline = \"red\", width = 1)\n",
    "\tdraw.text((xmin, ymin), f\"{label}: {round(score, 2)}\", fill = \"red\")\n",
    "\n",
    "im"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pipeline背后的实现"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "from transformers import *\n",
    "import torch"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "tokenizer = AutoTokenizer.from_pretrained(\"uer/roberta-base-finetuned-dianping-chinese\")\n",
    "model = AutoModelForSequenceClassification.from_pretrained(\"uer/roberta-base-finetuned-dianping-chinese\")"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "input_text = \"我觉得不太行！\"\n",
    "inputs = tokenizer(input_text, return_tensors = \"pt\")\n",
    "inputs"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "res = model(**inputs)\n",
    "res"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "logits = res.logits\n",
    "logits = torch.softmax(logits, dim = -1)\n",
    "logits"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "pred = torch.argmax(logits).item()\n",
    "pred"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "model.config.id2label"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "result = model.config.id2label.get(pred)\n",
    "result"
   ],
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "transformers",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
